OPT = -W
CC  = erlc
VSN = 0.0.1

ESRC = src
EBIN = ebin
EDOC= doc

SRC = $(wildcard $(ESRC)/*.erl)
TARGET = $(addsuffix .beam, $(basename \
             $(addprefix $(EBIN)/, $(notdir $(SRC)))))

SEP :=  ","
EMPTY:=
SPACE:= $(EMPTY) $(EMPTY)
DOC_SRC = $(addsuffix ", $(addprefix ", $(strip $(subst $(SPACE),$(SEP),$(SRC)))))

TEST_SRC = $(wildcard $(ESRC)/*_test.erl)

all: build test docs

build: bindir $(TARGET)

bindir:
	-mkdir $(EBIN)

docs: docdir clean_docs
	erl -noshell -run edoc_run files '[$(DOC_SRC)]' '[{dir, "$(EDOC)"}, {new, true}]'

docdir:
	-mkdir $(EDOC)

test:
	erl -noshell -pa $(EBIN) -eval 'eunit:test("$(EBIN)",[verbose])' -s init stop

$(EBIN)/%.beam: $(ESRC)/%.erl
	$(CC) $(OPT) -o $(EBIN) $<

.PHONY: clean clean_docs

clean:
	- rm -Rf ebin/*.beam

clean_docs: 
	- rm -Rf doc/*.*
	- cp $(ESRC)/*.edoc $(EDOC)/